/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package monety;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Jedrek
 */
public class Monety {

    MonetyWnd monetyWnd;
    ArrayList<Coin> coins;
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        Monety monety = new Monety();
    }
    
    public Monety(){
        
        coins = new ArrayList<Coin>();
        
        monetyWnd = new MonetyWnd(this);
        monetyWnd.setVisible(true);
    }
    
    public void addCoin(String name, int year){
        Coin c = new Coin(name, year);
        Coin coin;
        
        coin = findExact(c);
        if( coin!=null ){
            coin.incCount();
        } else{
            coin = c;
            coins.add(coin);
        }
        
        monetyWnd.showCoins(coins);
    }
    
    public void addCoin(Coin c){
        Coin coin;
        
        coin = findExact(c);
        if( coin!=null ){
            coin.incCount();
        } else{
            coin = c;
            coins.add(coin);
        }
        
        monetyWnd.showCoins(coins);
    }
    
    public boolean deleteCoin(Coin c){
        Coin coin;
        
        coin = findExact(c);
        if( coin == null ){
            return false;
        }
        
        if( coin.getCount() == 1 )
            coins.remove(coin);
        else
            coin.decCount();
        
        monetyWnd.showCoins(coins);
        return true;
    }
    
    public void saveList(String name){
        FileOutputStream fos;
        try {
            fos = new FileOutputStream(name);
        } catch (FileNotFoundException ex) {
            JOptionPane.showMessageDialog(null, "Couldn's save file", "Error!", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        PrintStream ps = new PrintStream(fos);
        
        for( int i = 0; i<coins.size(); i++){
            ps.println(coins.get(i).getName() + "\t" + coins.get(i).year + "\t" + coins.get(i).getCount());
        }
        
        ps.close();
        try {
            fos.close();
        } catch (IOException ex) {
        }
    }
    
    public boolean loadList(String name){
        
        FileInputStream fis;
        String line = null;
        String [] words;
        
        try {
            fis = new FileInputStream(name);
        } catch (FileNotFoundException ex) {
            JOptionPane.showMessageDialog(null, "Couldn's save file", "Error!", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        
        BufferedReader br = new BufferedReader(new InputStreamReader(fis));
        
        coins.clear();
        try{
            while( (line=br.readLine())!=null && line.length()>0){
                line.trim();
                if(line.length() == 0)
                    break;

                try{
                    words = line.split("\t");
                    if(words != null && words.length==3){
                        Coin coin = new Coin();
                        coin.setName(words[0]);
                        coin.year = Integer.parseInt(words[1]);
                        coin.setCount(Integer.parseInt(words[2]));
                        
                        coins.add(coin);
                    }
                } catch (Exception e){
                    continue;
                }
            }
        } catch (Exception e){}
        
        monetyWnd.showCoins(coins);        
        try {
            br.close();
            fis.close();
        } catch (IOException ex) {
        }
        
        return true;
    }
    
    public void sortByName(){
        boolean end = false;
        Coin coin;
        
        while(!end){
            end = true;
            for( int i = 0; i<coins.size()-1; i++){
                if(coins.get(i).getName().compareToIgnoreCase(coins.get(i+1).getName())>0){
                    coin = coins.get(i);
                    coins.remove(i);
                    coins.add(i+1, coin);
                    end = false;
                }
            }
        }
        
        monetyWnd.showCoins(coins);
    }
    
    public void sortByYear(){
        boolean end = false;
        Coin coin;
        
        while(!end){
            end = true;
            for( int i = 0; i<coins.size()-1; i++){
                if(coins.get(i).year > coins.get(i+1).year){
                    coin = coins.get(i);
                    coins.remove(i);
                    coins.add(i+1, coin);
                    end = false;
                }
            }
        }
        
        monetyWnd.showCoins(coins);
    }
    
    public void sortByCount(){
        boolean end = false;
        Coin coin;
        
        while(!end){
            end = true;
            for( int i = 0; i<coins.size()-1; i++){
                if(coins.get(i).getCount() < coins.get(i+1).getCount()){
                    coin = coins.get(i);
                    coins.remove(i);
                    coins.add(i+1, coin);
                    end = false;
                }
            }
        }
        
        monetyWnd.showCoins(coins);
    }
    
    public Coin findExact(Coin coin){
        
        Coin c;
        for( int i=0; i<coins.size(); i++ ){
            c = coins.get(i);
            if(c.getName().compareTo(coin.getName()) == 0 && c.year == coin.year){
                return c;
            }
        }
              
        return null;
    }
    
    public ArrayList<Coin> find(Coin coin, ArrayList<Float> correlation){
        ArrayList<Coin> result = new ArrayList<Coin>();
        Coin c;
        
        for(int i = 0; i<coins.size(); i++){
            c = coins.get(i);
            if(c.getName().compareTo(coin.getName())==0 && c.year == coin.year)
                correlation.add(new Float(1.0f));
            else{
                correlation.add( new Float(
                        0.34f*c.compareHist(coin.getHistogram()) +
                        0.1f*c.compareYear(coin.year) + 
                        0.5f*c.compareMarkov(coin.getMarkov())
                        )
                        );
            }
            result.add(coins.get(i));
        }
        
        //sorting
        boolean end = false;
        Float cor;
        
        while(!end){
            end = true;
            for( int i = 0; i<correlation.size()-1; i++){
                if((float)correlation.get(i) < (float)correlation.get(i+1)){
                    c = result.get(i);
                    result.remove(i);
                    result.add(i+1, c);
                    cor = (float)correlation.get(i);
                    correlation.remove(i);
                    correlation.add(i+1, cor);
                    end = false;
                }
            }
        }
        
        return result;
    }
}
